#include "SpriteScene.h"
using namespace cocos2d;
#define TEST_COUNT 1

#define CreateTestCreateMethod(ClassName) \
CCLayer * create##ClassName(){ return new ClassName;}

typedef CCLayer*(*TestCreateMethod)();

CreateTestCreateMethod(TouchAddSprite);

const TestCreateMethod g_aCreateFunctions[TEST_COUNT]=
{
	createTouchAddSprite,
};

//

bool SpriteScene::init()
{
	bool ret = false;
	do 
	{
		testLayer = NULL;
		curTestIndex=0;
		CC_BREAK_IF(!CCScene::init());
		rootLayer = CCLayer::create();
		this->addChild(rootLayer);


		CCSprite *arrowLeft = CCSprite::create("arrows.png",CCRect(0,0,30,42));
		CCSprite *arrowRight = CCSprite::create("arrows.png",CCRect(14,0,30,42));
		CCMenuItemImage * backMenuItem = CCMenuItemImage::create("btn-back.png",NULL,NULL,this,menu_selector(SpriteScene::menuCallbackGoBack));
		CCMenuItemSprite *preMenuItem = CCMenuItemSprite::create(arrowLeft,NULL,NULL,this,menu_selector(SpriteScene::menuCallbackPreSpriteTest));
		CCMenuItemSprite *nextMenuItem = CCMenuItemSprite::create(arrowRight,NULL,NULL,this,menu_selector(SpriteScene::menuCallbackNextSpriteTest));

		CCMenu* menu = CCMenu::create(preMenuItem, nextMenuItem, backMenuItem, NULL);
		rootLayer->addChild(menu);
		
		CCSize  menuSize =  backMenuItem->getContentSize();
		CCSize winSize = CCDirector::sharedDirector()->getWinSize();
		menu->setContentSize(winSize);
		menu->setPosition(ccp(winSize.width/2,winSize.height/2));

		backMenuItem->setPosition(ccp( - winSize.width / 2 + menuSize.width / 2 ,winSize.height / 2 - menuSize.height / 2));
		preMenuItem->setPosition(ccp(- winSize.width / 2 + 15,-winSize.height / 2 + 21));
		nextMenuItem->setPosition(ccp(winSize.width / 2 - 15,-winSize.height / 2 + 21));




		CCLabelTTF* label = CCLabelTTF::create("Sprite test", "Arial", 20);
		rootLayer->addChild(label, 1);
		CCSize labelSize = label->getContentSize();
		label->setPosition(ccp(winSize.width / 2, winSize.height - labelSize.height));

		showTest();
		ret = true;
	} while (false);
	return ret;
}

cocos2d::CCScene * SpriteScene::scene()
{
	return SpriteScene::create();
}

void SpriteScene::menuCallbackGoBack( cocos2d::CCObject * pSender )
{
	CCDirector::sharedDirector()->popScene();
}

void SpriteScene::menuCallbackPreSpriteTest( cocos2d::CCObject * pSender )
{
	curTestIndex--;
	if(curTestIndex<0)
	{
		curTestIndex += TEST_COUNT;
	}
	showTest();
}

void SpriteScene::menuCallbackNextSpriteTest( cocos2d::CCObject * pSender )
{
	curTestIndex ++ ;
	curTestIndex = curTestIndex % TEST_COUNT;
	showTest();
}

void SpriteScene::showTest()
{
	if(testLayer)
	{
		this->removeChild(testLayer);
	}
	TestCreateMethod createMethod = g_aCreateFunctions[curTestIndex];
	testLayer = createMethod();
	testLayer->autorelease();
	this->addChild(testLayer);
}

//TouchAddSprite Test
TouchAddSprite::TouchAddSprite()
{
	this->setTouchEnabled(true);
	testMoveSprite = CCSprite::create("ball.png");
	this->addChild(testMoveSprite);
}

void TouchAddSprite::ccTouchesEnded( CCSet* touches, CCEvent* event )
{
	CCSetIterator it;
	CCTouch* touch;
	for( it = touches->begin(); it != touches->end(); it++) 
	{
		touch = (CCTouch*)(*it);
		if(!touch)
			break;
		CCPoint location = touch->getLocation();
		CCActionInterval*  actionTo = CCMoveTo::create(0.8,location);
		testMoveSprite->runAction(actionTo);
	}
}
